const Task = require("./index.js")
const request = Task.request
const newUrl = Task.newUrl
const proxyErrorHandler = Task.proxyErrorHandler

/**
 * 请求得到用户信息
 * @param session
 */
Task.prototype.getUserInfo = function *(session){
    var res = yield request.get(newUrl("user/info")).set("Cookie",`LoginToken=${session}`)       // promise reject yield return undefined when HTTP 400
    return JSON.parse(res.text)
}

/**
 * 根据cookie得到用户
 * @param ctx
 * @returns {*}
 */
Task.prototype.getUserByCookie = function *(){
    let self = this
    let ctx = this.ctx
    // get session
    var session = ctx.cookies.get("LoginToken")
    if(typeof session == "undefined" || session == null){
        return "NULL"
    }

    // check session
    var res = yield self.getUserInfo(session)
    if(res.msg.error){
        return "NULL"
    }

    return res.msg
}